
import { GameplayComponent } from "../../GameplayComponent";
import ViewGameplay from "../../ViewGameplay";
import { Comp } from "../../m/CompDef";
import { GpEventDef } from "../../m/GpEventDef";
import { EHeroAttri } from "../../m/GpDef";
import { DamageEventInfo } from "../../m/systems/battle/BattleSystem";
import { GpEntity } from "../GpEntity";
import GpHero from "../GpHero";
import { GpWorldLayer } from "../GpWorld";
import GpDamageLabel, { GpDamageLabelProp } from "./GpDamageLabel";


import { _decorator, Component, Node, Prefab, UITransform } from 'cc';
import { NodePoolMap } from "../../../../../extensions/cocos-scaffold-3x/assets/ui/NodePoolMap";
import { EBundleName, gg } from "../../../../Game";
const { ccclass, property } = _decorator;
@ccclass
export default class GpDamageLabelMaker extends GameplayComponent<ViewGameplay>{

    @property(Prefab)
    labelPrefab: Prefab = null;

    labelParent: Node = null;

    nodePoolMap: NodePoolMap = new NodePoolMap(GpDamageLabel, EBundleName.subBundle);

    onInit(): void {
        this.labelParent = this.prop.gpWorld.getLayer(GpWorldLayer.damageLabel);
        const gpWorld = this.prop.gpWorld
        gg.eventTarget.on(GpEventDef.onDamage, (eventInfo: DamageEventInfo) => {
            if (eventInfo.target.component(Comp.Force).value == 0) {
                return
            }
            const gpEntity = gpWorld.entityMap.get(eventInfo.target.id)
            if (gpEntity) {
                const battleAttri = gpEntity.ecsEntity.component(Comp.Battle.Attri)

                this.makeLabel(gpEntity, eventInfo.value)
            }
        }, this)
    }

    async makeLabel(entity: GpEntity, damage: number) {
        const node = await this.nodePoolMap.makeNode(GpDamageLabel.path)
        const gpHero = entity.getComponent(GpHero)
        const startY = entity.node.position.y + gpHero.ske.node.getComponent(UITransform).height * gpHero.ske.node.scale.y


        node.active = true
        node.setPosition(entity.node.position.x, startY, 0)

        node.getComponent(GpDamageLabel).init(new GpDamageLabelProp(this, damage))

        if (!node.parent) {
            this.labelParent.addChild(node);
        }

    }
}